﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Business;
using BitPortal.Controls;
using BitPortal.Utils;
using BitPortal.Models;
using System.Web.UI.HtmlControls;
using System.Web.Security;

namespace BitPortal
{
    public partial class BitMasterPage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            //Load Banner & Favicon
            SetFavIcon();
            TopBanner1.Url = PortalConfigBLL.GetValueByName("Web_TopBanner").ToString();
            //Load OnAllPage Modules
            foreach (Models.Module module in ModuleBLL.GetModuleByCondition<Int32>(onAllPage: true, isEnabled: true))
            {
                BaseUserControl c = (BaseUserControl)LoadControl(module.GetPath());
                c.ReferenceModule = module;
                if (module.Position == "LeftContent")
                {
                    this.LeftPane.Controls.Add(c);
                }
                else if (module.Position == "RightContent")
                {
                    this.RightPane.Controls.Add(c);
                }
                else if (module.Position == "CenterContent")
                {
                    this.CenterPane.Controls.Add(c);
                }
                else if (module.Position == "TopContent")
                {
                    this.TopPane.Controls.Add(c);
                }
                else if (module.Position == "BottomContent")
                {
                    this.BottomPane.Controls.Add(c);
                }
            }

            Models.PortalPage page = PageBLL.GetClassName(this.Page.GetType().Name);
            if (page == null)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["PageID"]))
                {
                    page = PageBLL.GetByID(Request.QueryString["PageID"].ToInt32TryParse());
                }
            }
            if (page != null)
            {
                //Load Layout
                switch (page.Layout)
                {
                    case "LeftSidebar":
                        RightSidebar.Visible = false;
                        break;
                    case "RightSidebar":
                        LeftSidebar.Visible = false;
                        break;
                    case "OneColumn":
                        RightSidebar.Visible = LeftSidebar.Visible = false;
                        break;
                }

                //Load Page's Modules
                foreach (Models.Module module in ModuleBLL.GetModuleByCondition<Int32>(pageId: page.PageID, onAllPage: false, isEnabled: true))
                {
                    BaseUserControl c = (BaseUserControl)LoadControl(module.GetPath());
                    c.ReferenceModule = module;
                    if (module.Position == "LeftContent")
                    {
                        this.LeftPane.Controls.Add(c);
                    }
                    else if (module.Position == "RightContent")
                    {
                        this.RightPane.Controls.Add(c);
                    }
                    else if (module.Position == "CenterContent")
                    {
                        this.CenterPane.Controls.Add(c);
                    }
                    else if (module.Position == "TopContent")
                    {
                        this.TopPane.Controls.Add(c);
                    }
                    else if (module.Position == "BottomContent")
                    {
                        this.BottomPane.Controls.Add(c);
                    }
                }
            }

        }
        protected void SetFavIcon()
        {
            object favicon = PortalConfigBLL.GetValueByName("Web_Favicon");
            if (favicon != null)
            {
                HtmlLink htmlLink1 = new HtmlLink();
                htmlLink1.Attributes["rel"] = "icon";
                htmlLink1.Attributes["href"] = favicon.ToString();

                HtmlLink htmlLink2 = new HtmlLink();
                htmlLink2.Attributes["rel"] = "shortcut icon";
                htmlLink2.Attributes["href"] = favicon.ToString();

                Page.Header.Controls.Add(htmlLink1);
                Page.Header.Controls.Add(htmlLink2);
            }
        }
    }
}